package org.jeecg.modules.job.sub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.job.sub.entity.SubAnswer;
import org.jeecg.modules.job.sub.entity.SubAnswerRecords;
import org.jeecg.modules.job.sub.mapper.SubAnswerMapper;
import org.jeecg.modules.job.sub.service.ISubAnswerRecordsService;
import org.jeecg.modules.job.sub.service.ISubAnswerService;
import org.jeecg.modules.job.sub.service.ISubPhaseRecordsService;
import org.springframework.stereotype.Service;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Description: 题目答案
 * @Author: jeecg-boot
 * @Date:   2022-10-04
 * @Version: V1.0
 */
@Service
public class SubAnswerServiceImpl extends ServiceImpl<SubAnswerMapper, SubAnswer> implements ISubAnswerService {
	
	@Resource
	private SubAnswerMapper subAnswerMapper;
	@Resource
	private ISubAnswerRecordsService answerRecordsService;
	@Resource
	private ISubPhaseRecordsService phaseRecordsService;
	
	@Override
	public List<SubAnswer> selectByMainId(String mainId) {
		return subAnswerMapper.selectByMainId(mainId);
	}


	@Transactional
	@Override
	public boolean answerQuestion(SubAnswer answer) {
		//添加答题记录
		SubAnswerRecords records=new SubAnswerRecords();
		records.setAnswer(answer.getAnswer());
		records.setPhaseId(answer.getPhaseId());
		records.setTitleId(answer.getTitleId());
		records.setUserId(answer.getUserId());
		//查询答案
		SubAnswer result=subAnswerMapper.selectOne(new QueryWrapper<>(new SubAnswer().setTitleId(answer.getTitleId())));
		if (result.getAnswer().equals(answer.getAnswer())){
			records.setResult(1);
			answerRecordsService.addOrUpdate(records);
			//是否最后一题
			if (answer.getIsLast()==1){
				//生成阶段结果
				phaseRecordsService.createPhaseRecord(answer.getUserId(),answer.getPhaseId());
			}
			return true;
		}
		records.setResult(0);
		answerRecordsService.addOrUpdate(records);
		//是否最后一题
		if (answer.getIsLast()==1){
			//生成阶段结果
			phaseRecordsService.createPhaseRecord(answer.getUserId(),answer.getPhaseId());
		}
		return false;
	}
}
